package com.gcsoftware.web.service;

import com.gcsoftware.web.mapper.T_COM_SYS_LOGMAPPER;
import com.gcsoftware.authentications.beans.UserInfo;
import com.gcsoftware.web.beans.T_COM_SYS_LOGS;
import com.gcsoftware.web.mapper.T_COM_SYS_USERMAPPER;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.utils.GenerateIDs;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qs on 2018/3/25.
 */
@Service
public class T_COM_SYS_LOGSERVICE {
    @Autowired
    private T_COM_SYS_LOGMAPPER mapper;

    @Autowired
    private T_COM_SYS_USERMAPPER userMapper;

    public int insertSysLog(T_COM_SYS_LOGS log) {
        return mapper.insertSysLog(log);
    }

    public int getMaxId() {
        String maxLogId = mapper.getMaxId();
        if (StringUtils.isNotBlank(maxLogId)) {
            return Integer.parseInt(maxLogId.replace(GenerateIDs.ID_TYPE_LOG_ID,""));
        }
        return 0;
    }

    public String insertSysLog(String logType, String description , String logStatus) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo user = (UserInfo) subject.getSession().getAttribute("user");

        return insertSysLog(logType,description,logStatus,user.getUserId(),user.getLoginId());
    }

    public String insertSysLog(String logType, String description , String logStatus , String c_USER_ID , String c_LOGIN_ID) {
        T_COM_SYS_LOGS log = new T_COM_SYS_LOGS();

        if(0 == GenerateIDs.getMaxLogId()) {
            GenerateIDs.setMaxLogId(getMaxId());
        }
        log.setC_LOG_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_LOG_ID));
        log.setC_OPERATE_DETAIL(description);
        log.setC_LOGIN_IP(LogUtils.getCliectIp());
        log.setC_LOG_TYPE(logType);
        log.setC_LOGIN_ID(c_LOGIN_ID);
        log.setC_USER_ID(c_USER_ID);
        log.setC_LOG_STATUS(logStatus);
        insertSysLog(log);
        return log.getC_LOG_ID();
    }

    public List<T_COM_SYS_LOGS> queryLoginLogs( String c_LOGIN_NAME,  String c_START_TIME,  String c_END_TIME) {
        return mapper.queryLoginLogs(c_LOGIN_NAME,c_START_TIME,c_END_TIME);
    }

    public List<T_COM_SYS_LOGS> queryOprLogs(String c_LOGIN_ID)
    {
        return mapper.queryOprLogs(c_LOGIN_ID);
    }

}
